#!/usr/bin/env node
var args = process.argv.splice(2, process.argv.length - 2);
if (args.length == 0) process.exit();

var request = require('request');
var util = require('util');
String.prototype.padLeft = function(l,c) {return Array(l-this.length+1).join(c||" ")+this}

var urls = [
	'http://api.map.baidu.com/location/ip?ak=F454f8a5efe5e577997931cc01de3974&ip=%s',
	'http://ip.taobao.com/service/getIpInfo.php?ip=%s',
	'http://ip.taobao.com/service/getIpInfo2.php?ip=%s',
	'http://freeapi.ipip.net/%s',
	'http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip=%s'
];
var apis = {
	baidu: function getBaidu(ip) {
		request(util.format(urls[0], ip), function (error, response, body) {
			if (!error && response.statusCode == 200) {
				//console.log(body);
				body = body && body.indexOf('{') == 0 ? JSON.parse(body) : null;
				if (body && body.content && body.content.address_detail) {
					var point = body.content.point || null;
					body = body.content.address_detail;
					console.log(ip.padLeft(15, ' '), ':', body.province, body.city, body.district, point, 'baidu');
				}
				list.splice(0, 1);
			}
			index++;
			get();
		})
	},
	taobao: function getTaobao(ip) {
		request(util.format(urls[1], ip), function (error, response, body) {
			if (!error && response.statusCode == 200) {
				//console.log(body);
				body = body && body.indexOf('{') == 0 ? JSON.parse(body) : null;
				if (body && body.code == 0) {
					body = body.data;
					console.log(ip.padLeft(15, ' '), ':', body.country, body.region, body.city, body.county, 'taobao');
				}
				list.splice(0, 1);
			}
			index++;
			get();
		})
	},
	taobao2: function getTaobao2(ip) {
		request(util.format(urls[2], ip), function (error, response, body) {
			if (!error && response.statusCode == 200) {
				//console.log(body);
				body = body && body.indexOf('{') == 0 ? JSON.parse(body) : null;
				if (body && body.code == 0) {
					body = body.data;
					console.log(ip.padLeft(15, ' '), ':', body.country, body.region, body.city, body.county, 'taobao2');
				}
				list.splice(0, 1);
			}
			index++;
			get();
		})
	},
	ipip: function getIpIp(ip) {
		request(util.format(urls[3], ip), function (error, response, body) {
			if (!error && response.statusCode == 200) {
				//console.log(body);
				body = body && body.indexOf('[') == 0 ? JSON.parse(body) : null;
				if (body && body.length > 0) {
					console.log(ip.padLeft(15, ' '), ':', body[0], body[1], body[2], body[3], 'ipip');
				}
				list.splice(0, 1);
			}
			index++;
			get();
		})
	},
	sina: function getSina(ip) {
		request(util.format(urls[4], ip), function (error, response, body) {
			if (!error && response.statusCode == 200) {
				body = body.replace('var remote_ip_info = ', '');
				body = body.substr(0, body.length - 1);
				//console.log(body);
				body = body && body.indexOf('{') == 0 ? JSON.parse(body) : null;
				if (body && body.ret == 1) {
					console.log(ip.padLeft(15, ' '), ':', body.country, body.province, body.city, body.district, 'sina');
				}
				list.splice(0, 1);
			}
			index++;
			get();
		})
	}
};

var list = null, keys = {}, index = 0, use = [];
if (args.length > 1) {
	var tmp = args[0].split('#');
	for(var k in apis) if (tmp.indexOf(k) != -1 && use.indexOf(k) == -1 && k.length>0) use.push(k);
	if (use.length > 0) args.splice(0, 1);
}
if (use.length == 0) for(var k in apis) use.push(k);

for(var i in args) {
	list = args[i].match(/((\d+)\.(\d+)\.(\d+)\.(\d+))/ig);
	if (list && list.length>0) for(var j in list) if (!keys[list[j]]) keys[list[j]] = true;
}
list = []; for(var ip in keys) list.push(ip);

function get() {
	if (list.length == 0) process.exit();
	var ip = list[0];
	apis[use[index%use.length]](ip);
};

get();
